package pri.damai.xiaowu.anydoor.business.group;

import com.google.common.collect.Lists;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.Data;
import pri.damai.xiaowu.anydoor.constant.ChatConst;
import pri.damai.xiaowu.anydoor.business.BaseModel;
import pri.damai.xiaowu.anydoor.netty.ChannelStore;

import java.util.concurrent.ThreadLocalRandom;

/**
 * 创建群组
 *
 * @description
 * @author YangXinFu
 * @date 15:12 2022/7/25
 **/
@Data
public class CreateGroupReq implements BaseModel {

    private String groupName;

    @Override
    public Byte code() {
        return 11;
    }

    @Override
    public void process(ChannelHandlerContext ctx) {
        int groupId = ThreadLocalRandom.current().nextInt();
        ChannelStore.GROUP_NAME_MAP.put(groupId + "", this.getGroupName());
        Object userId = ctx.channel().attr(AttributeKey.valueOf(ChatConst.USER_ID_KEY)).get();
        ChannelStore.GROUP_USER_MAP.put(groupId + "",
                Lists.newArrayList((Integer) userId));

        CreateGroupRsp rsp = new CreateGroupRsp();
        rsp.setGroupId(groupId);
        rsp.setGroupName(this.getGroupName());
        ctx.channel().writeAndFlush(rsp);
    }
}
